package com.bawei.yangzongcai___chenmei.SlindingMenu;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.bawei.yangzongcai___chenmei.BaseAddter.Dlisengyer;
import com.bawei.yangzongcai___chenmei.Adpter.QAQadpterActivity;
import com.bawei.yangzongcai___chenmei.JavaClass.JavaLianxi;
import com.bawei.yangzongcai___chenmei.MVC.BsaeActivity;
import com.bawei.yangzongcai___chenmei.MVC.base.HttpMannoss;
import com.bawei.yangzongcai___chenmei.MVC.base.MVCmoul;
import com.bawei.yangzongcai___chenmei.R;
import com.bawei.yangzongcai___chenmei.SQL.SQLyongyong;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemChildLongClickListener;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.io.IOException;
import java.util.List;

import jp.wasabeef.recyclerview.animators.LandingAnimator;
import okhttp3.Response;

public class SlingMenuActivity1 extends BsaeActivity implements OnItemChildClickListener, OnItemChildLongClickListener, OnRefreshLoadMoreListener, HttpMannoss.MayCallback {


    private SmartRefreshLayout samer;
    private RecyclerView review;
    private boolean isoffer=true;
    private int pash=3;
    private QAQadpterActivity qadpterActivity;
    private MVCmoul mvCmoul;
    private Button tianjia;
    private Button shanchu;

    @Override
    public int bandLayout() {
        return R.layout.activity_sling_menu1;
    }

    @Override
    public void initView() {
        samer = findViewById(R.id.samer);
        review = findViewById(R.id.review);
        review.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL));
        samer.setOnRefreshLoadMoreListener(this);
        tianjia = findViewById(R.id.tianjia);
        shanchu = findViewById(R.id.shanchu);
        review.setItemAnimator(new LandingAnimator());
        review.getItemAnimator().setRemoveDuration(200);
        review.getItemAnimator().setAddDuration(200);
        review.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        review.addItemDecoration(new Dlisengyer(this));

    }

    @Override
    public void initDate() {
      mvCmoul = new MVCmoul();
      mvCmoul.reastarLayout(pash,this);
      tianjia.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              qadpterActivity.notifyItemInserted(1);
          }
      });

        shanchu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                qadpterActivity.notifyItemRemoved(1);
            }
        });

    }

    public void initLayoution(List<JavaLianxi.DataBean> lianxi)
    {
        samer.finishLoadMore();
        samer.finishRefresh();
        if (qadpterActivity==null)
        {
            qadpterActivity = new QAQadpterActivity(lianxi);
            qadpterActivity.setOnItemChildClickListener(this);
            qadpterActivity.setOnItemChildLongClickListener(this);
            review.setAdapter(qadpterActivity);
            qadpterActivity.addHeaderView(initbanner(lianxi));

        }else {
            if (isoffer)
            {
                qadpterActivity.getData().clear();
            }
            qadpterActivity.getData().addAll(lianxi);
            qadpterActivity.notifyDataSetChanged();
        }
    }

    public Banner initbanner(List<JavaLianxi.DataBean> javabanner)
    {
        Banner banner = new Banner(this);
        banner.setLayoutParams(new  ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, SizeUtils.dp2px(500)));
        banner.setImages(javabanner);
        banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                JavaLianxi.DataBean javaLianxi = (JavaLianxi.DataBean) path;
                Glide.with(context).load(javaLianxi.getUrl()).into(imageView);
            }
        });
        banner.start();
        return banner;
    }

    @Override
    public void suuess(Response response) {
        JavaLianxi javaLianxi = null;
        try {
            javaLianxi = GsonUtils.fromJson(response.body().string(),JavaLianxi.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        int coot=0;
        List<JavaLianxi.DataBean> dataBeans= javaLianxi.getData();
        for (JavaLianxi.DataBean dataBean : dataBeans) {
            dataBean.setTabyLayout(++coot);
            if (coot==3)
            {
                coot=0;
            }
        }
        initLayoution(dataBeans);
    }

    @Override
    public void fial(String smg) {

    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()){
            case R.id.yangchen3_text1:
                ToastUtils.showShort(""+qadpterActivity.getData().get(position).getTitle());
                break;

            case R.id.yangchen3_text3:
                ToastUtils.showShort(""+qadpterActivity.getData().get(position).getDesc());
                break;

        }
    }

    @Override
    public boolean onItemChildLongClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId())
        {
            case R.id.yangchen3_img:
                ToastUtils.showShort("已缓存到数据库："+qadpterActivity.getData().get(position).getAuthor());
                SQLyongyong sqLyongyong= new SQLyongyong(this,"chenmei.dp",null,1);
                SQLiteDatabase read= sqLyongyong.getReadableDatabase();
                read.execSQL("insert into chenmei values(?,?,?)",new Object[]{
                        qadpterActivity.getData().get(position).getTitle(),
                        qadpterActivity.getData().get(position).getDesc(),
                        qadpterActivity.getData().get(position).getUrl()
                });
                break;
        }
        return true;
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        isoffer=false;
        pash++;
        if (pash==6)
        {
            pash=1;
        }
        mvCmoul.reastarLayout(pash,this);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        if (isoffer)
        {
            pash=1;
        }
        mvCmoul.reastarLayout(pash,this);
    }




}
